#include <iostream>
#include <cstring>
#include "Mque.h"
#include "Server.h"
using namespace std;

int main(int argc, char *argv[]) {
	int pid = fork();
	if (pid == 0) {
		sleep(3);
		char *socid = "/home/wocao/sj.soc";
		static struct sockaddr_un srv_addr;
		int connect_fd = socket(PF_UNIX, SOCK_STREAM, 0);
		if (connect_fd < 0) {
			perror("cannot create communication socket");
			return 1;
		}
		srv_addr.sun_family = AF_UNIX;
		strcpy(srv_addr.sun_path, socid);
		//connect server
		int ret = connect(connect_fd, (struct sockaddr*) &srv_addr,
				sizeof(srv_addr));
		if (ret == -1) {
			perror("cannot connect to the server");
			close(connect_fd);
			return 1;
		}
		string sco = "function:insert\nmqnam:niubi\ntype:11\ndate:sssss\n";
		string sco1 = "function:take\nmqnam:niubi\ntype:11\ndate:\n";
		char buf[4096] = { 0 };
		send(connect_fd, sco.c_str(), sco.length() + 1, 0);
		recv(connect_fd, buf, 4096, 0);
		string g(buf, 4096);
		cout << g << endl;
		send(connect_fd, sco1.c_str(), sco1.length() + 1, 0);
		recv(connect_fd, buf, 4096, 0);
		string g1(buf, 4096);
		cout << g1 << endl;
		close(connect_fd);
	} else {
		Mque mq;
		string *s = new string("ssss");
		string name = "niubi";
		mq.creatMque(name);
		Server ser(&mq);
		ser.start();
	}

//	mq.creatMque(name);
//	mq.insert(name, 1, (void*)s->c_str(),4);
//	cout<<(char*)(mq.get(name, 1).buf)<<endl;
//	cout<<(char*)(mq.take(name, 1).buf)<<endl;
//	cout<<(char*)(mq.get(name, 1).buf)<<endl;
	return 0;
}
